﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BasicComponents.pub
{
    /// <summary>
    /// Interaction logic for UC_LabelDatePicker.xaml
    /// </summary>
    public partial class UC_LabelDatePicker : UserControl
    {
        public UC_LabelDatePicker()
        {
            InitializeComponent();
        }
        TextChangedEventArgs textChangedEventArgs = null;
        public TextChangedEventArgs TextChangedEventArgs { get { return textChangedEventArgs; } set { textChangedEventArgs = value; } }

        //该参数保存输入框监测到的输入事件
        KeyEventArgs keyEventArgs = null;
        public KeyEventArgs KeyEventArgs
        {
            get { return keyEventArgs; }
            set { keyEventArgs = value; }
        }

        static void resetComponentSize(UC_LabelDatePicker sender)
        {
            // sender.Width = sender.column_title.Width.Value + sender.column_value.Width.Value + sender.columns_btn.Width.Value;
        }
        ////1、标题
        public String TitileVal
        {
            get { return (String)GetValue(TitileValProperty); }
            set { SetValue(TitileValProperty, value); }
        }
        public static readonly DependencyProperty TitileValProperty =
            DependencyProperty.Register("TitileVal", typeof(String), typeof(UC_LabelDatePicker), new PropertyMetadata("标题", (s, e) =>
            {
                UC_LabelDatePicker sender = s as UC_LabelDatePicker;
                sender.label_titile.Content = e.NewValue.ToString();
                // resetComponentSize(sender);
            }));

        //enable属性
        public Boolean DateIsEnabled
        {
            get { return (Boolean)GetValue(DateIsEnabledProperty); }
            set { SetValue(DateIsEnabledProperty, value); }
        }
        public static readonly DependencyProperty DateIsEnabledProperty =
            DependencyProperty.Register("DateIsEnabled", typeof(Boolean), typeof(UC_LabelDatePicker), new PropertyMetadata(true, (s, e) =>
            {
                UC_LabelDatePicker sender = s as UC_LabelDatePicker;
                sender.date_value.IsEnabled = (Boolean)e.NewValue; 

                // resetComponentSize(sender);
            }));
        //2、a、字符类型内容变化 
        public String S_DateVal
        {
            get { return (String)GetValue(S_DateValProperty); }
            set { SetValue(S_DateValProperty, value); }
        }
        public static readonly DependencyProperty S_DateValProperty = DependencyProperty.Register("S_DateVal", typeof(String), typeof(UC_LabelDatePicker), new PropertyMetadata("1990-01-01", (s, e) =>
        {
            UC_LabelDatePicker sender = s as UC_LabelDatePicker;
            try
            {
                DateTime dt;
                DateTimeFormatInfo df = new DateTimeFormatInfo();
                df.ShortDatePattern = "yyyyMMdd";
                try
                {
                    dt = DateTime.ParseExact(e.NewValue.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
                }
                catch (Exception e1)
                {
                    dt = DateTime.ParseExact(e.NewValue.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
                }
                sender.date_value.DisplayDate = dt;
                sender.date_value.SelectedDate = dt;
                sender.date_value.Text = e.NewValue.ToString();
            }
            catch (Exception e1)
            {
                File.AppendAllText("log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n日期组件中给定的日期转换失败，错误：" + e1.Message);
            }
        }));

        //2、b、字符类型内容变化 
        public DateTime D_DateVal
        {
            get { return (DateTime)GetValue(D_DateValProperty); }
            set { SetValue(D_DateValProperty, value); }
        }
        public static readonly DependencyProperty D_DateValProperty = DependencyProperty.Register("D_DateVal", typeof(DateTime), typeof(UC_LabelDatePicker), new PropertyMetadata(new DateTime(), (s, e) =>
        {
            UC_LabelDatePicker sender = s as UC_LabelDatePicker;
            try
            {
                sender.date_value.DisplayDate = (e.NewValue == null ? new DateTime() : (DateTime)e.NewValue);
                sender.date_value.SelectedDate = (e.NewValue == null ? new DateTime() : (DateTime)e.NewValue);
                sender.date_value.Text = (e.NewValue == null ? DateTime.Now.ToString("yyyy-MM-dd") : (((DateTime)e.NewValue).ToString("yyyy-MM-dd")));
            }
            catch (Exception e1)
            {
                File.AppendAllText("log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n日期组件中给定的日期赋值失败，错误：" + e1.Message);
            }
        }));

        //3、内容框长度 
        public Double TxtWidth
        {
            get { return (Double)GetValue(TxtWidthProperty); }
            set { SetValue(TxtWidthProperty, value); }
        }
        public static readonly DependencyProperty TxtWidthProperty = DependencyProperty.Register("TxtWidth", typeof(Double), typeof(UC_LabelDatePicker), new PropertyMetadata(120.00, (s, e) =>
        {
            UC_LabelDatePicker sender = s as UC_LabelDatePicker;
            sender.date_value.Width = Double.Parse(e.NewValue.ToString());
        }));

        //4、选择输入事件
        public event RoutedEventHandler DateValChanged
        {
            add { this.AddHandler(DateValChangedEvent, value); }
            remove { this.RemoveHandler(DateValChangedEvent, value); }
        }
        public static readonly RoutedEvent DateValChangedEvent = EventManager.RegisterRoutedEvent("DateValChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UC_LabelDatePicker));


        private void date_value_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                this.S_DateVal = this.date_value.SelectedDate.Value.ToString("yyyy-MM-dd");
                this.D_DateVal = this.date_value.SelectedDate.Value;
                SelectionChangedEventArgs args = new SelectionChangedEventArgs(UC_LabelDatePicker.DateValChangedEvent, e.AddedItems, e.RemovedItems);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
                File.AppendAllText("log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n选择日期事件错我:" + e1.Message);
            }
        }

        //按键按下事件
        //private void txt_value_KeyDown(object sender, KeyEventArgs e)
        //{
        //    try
        //    {
        //        this.keyEventArgs = e;
        //        RoutedEventArgs args = new RoutedEventArgs(UC_LabelDatePicker.TxtKeyDownEvent, this);
        //        RaiseEvent(args);
        //    }
        //    catch (Exception e1)
        //    {
        //        MessageBox.Show("" + e1.Message);
        //    }
        //}
    }
}
